//
//  MYGHealthManagementTopTabCell.swift
//  MYGrow
//
//  Created by 林矗 on 2024/3/9.
//

import UIKit

class MYGHealthManagementTopTabCell: UICollectionViewCell {
    // MARK: - Property
    var model: MYGHealthManagementTopTabCellModel? {
        didSet {
            guard let model = model else { return }
            titleLabel.text = model.title
            bgImageView.isHidden = !model.selected
            titleLabel.textColor = model.selected ? .white : .rgba(60, 60, 60)
            iconImageView.image = model.selected ? UIImage(named: model.iconImageName)?.withRenderingMode(.alwaysTemplate) : UIImage(named: model.iconImageName)?.withRenderingMode(.alwaysOriginal)
            cardView.backgroundColor = model.selected ? .clear : .white
        }
    }
        
    // MARK: - init
    override init(frame: CGRect) {
        super.init(frame: frame)
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - UI
    private func configUI() {
        contentView.addSubviews([
            bgImageView,
            cardView
        ])
        
        cardView.addSubviews([
            iconImageView,
            titleLabel
        ])
        
        bgImageView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        cardView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(cardView.snp.width).multipliedBy(67.5/78.5)
        }
        
        iconImageView.snp.makeConstraints { make in
            make.top.equalTo(13)
            make.centerX.equalToSuperview()
        }
        
        titleLabel.snp.makeConstraints { make in
            make.bottom.equalTo(-13)
            make.centerX.equalToSuperview()
        }
    }
    
    // MARK: - lazy load
    private lazy var bgImageView: UIImageView = {
        let imageView = UIImageView(image: UIImage(named: "Health/Management/tab_bg"))
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.isHidden = true
        return imageView
    }()
    
    private lazy var cardView: UIView = {
        let view = UIView()
        view.backgroundColor = .white
        view.layer.cornerRadius = 5
        view.clipsToBounds = false
        view.layerShadowColor = .rgba(0, 0, 0, 0.1)
        view.layerShadowOffset = CGSize(width: 0, height: 0)
        view.layerShadowOpacity = 1
        view.layerShadowRadius = 5
        return view
    }()
    
    private lazy var iconImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFill
        imageView.clipsToBounds = true
        imageView.tintColor = .white
        return imageView
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(13)
        label.textColor = .rgba(60, 60, 60)
        return label
    }()
}
